/*
 * @lc app=leetcode.cn id=309 lang=cpp
 *
 * [309] 最佳买卖股票时机含冷冻期
 */

// @lc code=start
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int n = prices.size();
        if (n == 1) return 0;
        vector<int> dp(2);
        
        dp[0] = max(0, prices[1] - prices[0]);
        dp[1] = max(- prices[0], - prices[1]);
        int dp_pre = 0;
        for (int i = 2; i < n; ++i) {
            int tmp = dp[0];
            dp[0] = max(dp[0], dp[1] + prices[i]);
            dp[1] = max(dp[1], dp_pre - prices[i]);
            dp_pre = tmp;
        }
        return dp[0];
    }
};
// @lc code=end

